<?php
	session_start();
	class FileManager{

		public static function FileExtension($filename){
			return end(explode(".", $filename));
		}

		public static function SaveFile($file){
			try{
			if (is_uploaded_file($file['tmp_name'])){
				$filename = $file['name'];
				while(file_exists(self::PathToSave().$filename)){
					$filename = '0'.$filename;
				}
				move_uploaded_file($file['tmp_name'], self::PathToSave().$filename);
				chmod(self::PathToSave().$filename, 0777 );
				return 	$filename;
			}else{
				return null;
			}
			}
			catch(Exception $e){
				echo $e->getMessage();	
			}
		}
		
		public static function CopyFile($oldname, $newname){

			if($oldname)
				if(file_exists(self::PathToSave().$oldname))
					if(Copy(self::PathToSave().$oldname, self::PathToSave().$newname.'.'.end(explode(".", $oldname)))){
//						echo 'Nuevo<br>';
						return $newname.'.'.end(explode(".", $oldname));
					}else{
//						echo 'Viejo<br>';
						return $oldname;
					}
				else
					return $oldname;
			return "";
		}


		public static function RenameFile($oldname, $newname){

			if($oldname)
				if(file_exists(self::PathToSave().$oldname))
					if(rename(self::PathToSave().$oldname, self::PathToSave().$newname.'.'.end(explode(".", $oldname)))){
//						echo 'Nuevo<br>';
						return $newname.'.'.end(explode(".", $oldname));
					}else{
//						echo 'Viejo<br>';
						return $oldname;
					}
				else
					return $oldname;
			return "";
		}


		public static function ReplaceFile($oldname, $newname){
			if($oldname)
				if(file_exists(self::PathToSave().$newname.'.'.end(explode(".", $oldname)))){
					unlink(self::PathToSave().$newname.'.'.end(explode(".", $oldname)));
					if(rename(self::PathToSave().$oldname, self::PathToSave().$newname.'.'.end(explode(".", $oldname))))
						return $newname.'.'.end(explode(".", $oldname));
					else
						return $oldname;
				}else{
					self::RenameFile($oldname,$newname);
				}
					
		}


		public static function PathToSave(){
			$myFile = get_include_path()."/libraries/php.conf";
			$fh = fopen($myFile, 'r');
			$theData = fread($fh, filesize($myFile));
			fclose($fh);
			$arr_data = split(",",$theData);
			return $arr_data[3];
		}
	}
?>